#include "ConnectMenu.hh"

ConnectMenu::ConnectMenu(Window &win) : GameScreen(win), _Window(win)
{
    this->_Font = new sf::Font();
    this->_Font->LoadFromFile("starcraft.ttf");
    this->maxCharServ = 0;
    this->maxCharCur = 0;

    sf::String  *IpServer = new sf::String("IP Serveur  :  ", *this->_Font, 30);
    IpServer->SetStyle(sf::String::Underlined);
    IpServer->SetColor(sf::Color(200, 128, 0));
    IpServer->SetPosition(this->_Window.GetWidth()/8.0, 50.0f);
    this->_Items.push_back(IpServer);

    sf::String  *IpClient = new sf::String("Votre IP  :  ", *this->_Font, 30);
    IpClient->SetPosition(this->_Window.GetWidth()/7.0, 150.0f);
    this->_Items.push_back(IpClient);

    sf::String  *Connection = new sf::String("Connexion", *this->_Font, 30);
    Connection->SetPosition(this->_Window.GetWidth()/3.0, 250.0f);
    this->_Items.push_back(Connection);

    sf::String  *Exit = new sf::String("Quitter", *this->_Font, 30);
    Exit->SetPosition(this->_Window.GetWidth()/2.7, 350.0f);
    this->_Items.push_back(Exit);

    this->_Current = this->_Items.begin();

    this->_ItemSelect.insert(std::pair<std::string, int>("IP Serveur", 0));
    this->_ItemSelect.insert(std::pair<std::string, int>("Votre IP", 1));
    this->_ItemSelect.insert(std::pair<std::string, int>("Connexion", MENU));
    this->_ItemSelect.insert(std::pair<std::string, int>("Quitter", EXIT));
}

ConnectMenu::~ConnectMenu()
{
}

int     ConnectMenu::Run(void)
{
    bool    isRunning = true;
    GameTime    *gameTime = new GameTime();
    Time        timer = 0.0f;
    sf::String  *item;

    while (isRunning)
    {
        timer += gameTime->ElapsedGameTime();
        sf::Event e;
        while (this->_Window.GetEvent(e))
        {
            item = *this->_Current;
            std::string t = item->GetText();

            if (t.find("IP Serveur") != -1 && this->maxCharServ > 15)
            {
                t = "IP Serveur : ";
                item->SetText(t);
                this->maxCharServ = 0;
            }
            if (t.find("Votre IP") != -1 && this->maxCharCur > 15)
            {
                t = "Votre IP : ";
                item->SetText(t);
                this->maxCharCur = 0;
            }

            if ((e.Type == sf::Event::Closed))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Escape))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Up))
                this->GoPrev();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Down))
                this->GoNext();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Return))
            {
                item = *this->_Current;
                std::string t = item->GetText();
                for (std::map<std::string, int>::iterator it = _ItemSelect.begin(); it != _ItemSelect.end(); ++it)
                {
                    if ((*it).first == t)
                        return (*it).second;
                }
            }
            else if ((e.Type == sf::Event::TextEntered) && t.find("IP Serveur") != -1)
            {
                char charEntered = static_cast<char>(e.Text.Unicode);
                if ((charEntered == '.' || (charEntered >= '0' && charEntered <= '9')) && this->maxCharServ < 16)
                {
                    this->maxCharServ++;
                    t += charEntered;
                    item->SetText(t);
                }
            }
            else if ((e.Type == sf::Event::TextEntered) && t.find("Votre IP") != -1)
            {
                char charEntered = static_cast<char>(e.Text.Unicode);
                if ((charEntered == '.' || (charEntered >= '0' && charEntered <= '9')) && this->maxCharCur < 16)
                {
                    this->maxCharCur++;
                    t += charEntered;
                    item->SetText(t);
                }
            }
        }
        this->Update(gameTime->ElapsedGameTime());
        this->Draw(gameTime->ElapsedGameTime());
    }
    return EXIT;
}

void    ConnectMenu::Update(Time ElapsedTime)
{

}

void    ConnectMenu::Draw(Time ElapsedTime)
{
    std::list<sf::String*>::iterator it;

    this->_Window.Clear();
    for (it = this->_Items.begin(); it != this->_Items.end(); ++it){
        this->_Window.Draw(*(*it));
    }
    this->_Window.Display();
}

void    ConnectMenu::GoNext(void)
{
    sf::String  *item;
    std::list<sf::String*>::iterator    tmp;

    tmp = this->_Current;
    ++tmp;
    if (tmp == this->_Items.end())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.begin();
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        if (this->_Current != this->_Items.end())
            ++this->_Current;
    }

    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

void    ConnectMenu::GoPrev(void)
{
    sf::String  *item;

    if (this->_Current == this->_Items.begin())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.end();
        --this->_Current;
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        --this->_Current;
    }
    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

int     ConnectMenu::Enter(void)
{
    return MENU;
}
